﻿using G19Glower.Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace G19Glower.App
{
    /// <summary>
    /// Interaction logic for SettingsWindow.xaml
    /// </summary>
    internal partial class SettingsWindow : Window
    {
        private SettingsManager mSettingManager;
        private ISettings[] mCurrentSettings;

        #region .ctor

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            // Make sure all settings UI is de-parented
            foreach (ISettings settings in mCurrentSettings)
            {
                var parentTabItem = (TabItem)settings.SettingsUI.Parent;
                parentTabItem.Content = null;
                settings.SettingsUIValueChanged -= settings_SettingsUIValueChanged;
            }

            this.SettingsTabControl.Items.Clear();
        }

        internal void Initialize(IEnumerable<GlowerDetails> glowies, SettingsManager settingsManager)
        {
            this.InitializeComponent();

            this.GlowiesListView.ItemsSource = glowies;

            mSettingManager = settingsManager;
            
            // Load settings
            mCurrentSettings = mSettingManager.GetSettings().ToArray();

            for (int index = 0; index < mCurrentSettings.Length; index++)
            {
                ISettings settings = mCurrentSettings[index];
                string settingsTitle = settings.SettingsTitle;
                UIElement settingsUI = settings.SettingsUI;

                if (!string.IsNullOrEmpty(settingsTitle) && settingsUI != null)
                {
                    var tabItem = new TabItem();
                    tabItem.Header = settingsTitle;
                    tabItem.Content = settingsUI;

                    this.SettingsTabControl.Items.Add(tabItem);

                    settings.SettingsUIValueChanged += settings_SettingsUIValueChanged;
                }
            }
        }

        #endregion

        #region General UI Event Handlers

        void settings_SettingsUIValueChanged(object sender, EventArgs e)
        {
            this.ApplyButton.IsEnabled = true;
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            this.ApplyButton.IsEnabled = true;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.ApplyAndSaveSettings();
            this.DialogResult = true;
            this.Close();
        }

        private void ApplyButton_Click(object sender, RoutedEventArgs e)
        {
            this.ApplyAndSaveSettings();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        #endregion

        #region Private Methods

        private void ApplyAndSaveSettings()
        {
            this.ApplyButton.IsEnabled = false;

            for (int index = 0; index < mCurrentSettings.Length; index++)
            {
                mCurrentSettings[index].ApplySettings();
            }

            var runningGlowies = new List<string>();
            foreach (GlowerDetails glower in (IEnumerable<GlowerDetails>)this.GlowiesListView.ItemsSource)
            {
                glower.ApplyDesiredRunningState();
                if (glower.DesiredRunningState)
                {
                    runningGlowies.Add(glower.Instance.GetType().FullName);
                }
            }

            mSettingManager.SetRunningGlowies(runningGlowies);

            try
            {
                mSettingManager.Save();
            }
            catch
            {
                // meh
            }
        }

        #endregion
    }
}